package com.briup.MR.funMR;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.RegexMapper;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.funMR.RegexMR -D input=/user/hdfs/regex/regex.txt -D output=/user/hdfs/regex/reg_result
 */
public class RegexMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        String input = conf.get("input");
        String output = conf.get("output");
        //设置正则表达式获取符合的数据
        conf.set("mapreduce.mapper.regex",".*com.*|^Dee.*");

        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("RegexMR");

        //RegexMapper要求进入map的值必须是Text
        //key是不处理，RegexMapper输出的键是正则匹配的内容
        //只是longWritable(1)
        job.setMapperClass(RegexMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setNumReduceTasks(0);

        TextInputFormat.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }
    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new RegexMR(),args));
    }
}
